#pragma once
#include "stdafx.h"
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/visualization/cloud_viewer.h>

class LitemViewer {
private:
	boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer;

	void keyboardEventOccurred (const pcl::visualization::KeyboardEvent &event, void* args) {
		//boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer = *static_cast<boost::shared_ptr<pcl::visualization::PCLVisualizer> *> (viewer_void);
		struct callback_args* data = (struct callback_args *)args;
		if (event.getKeySym() == "space" && event.keyDown ()) {
			cout << "switch modes" << std::endl;
			bool ch = *data->sleepKey;
			//*(data->sleepKey) = !ch;
		}
	}
	
	struct callback_args {
	  // structure used to pass arguments to the callback function
	  bool * sleepKey;
	  pcl::visualization::PCLVisualizer::Ptr viewerPtr;
	};
	
	bool stop;
	bool sleep;

public:
	LitemViewer() : sleep(false), stop(false) {
		viewer = pcl::visualization::PCLVisualizer::Ptr(new pcl::visualization::PCLVisualizer("LITEM"));
		
		//keyboard callback parameters
		struct callback_args cb_args;
		cb_args.sleepKey = &sleep;
		cb_args.viewerPtr = viewer;

		viewer->registerKeyboardCallback(&LitemViewer::keyboardEventOccurred, *this, (void*)&cb_args);
		//viewer->setBackgroundColor (0, 0, 0);
		viewer->setCameraPosition(0,0,-2,0,-1,0,0);
		//viewer->initCameraParameters ();
		//viewer->addCoordinateSystem (100.0);
	};

	bool isAsleep() {
		return sleep;
	}

	bool stopped() {
		return viewer->wasStopped();
	}

	int view(const LITEM_CLOUD::ConstPtr & cloud, string view_name = "input") {
		viewer->removePointCloud(view_name);
		viewer->removeAllShapes();
		pcl::visualization::PointCloudColorHandlerRGBField<LITEM_POINT> rgb(cloud);
		viewer->addPointCloud<LITEM_POINT>(cloud, rgb, view_name);
		viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, view_name);
		viewer->spinOnce (100);
		return 0;
	}
};